package com.cj.cms.service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cj.cms.dao.security.ChannelDao;
import com.cj.cms.entity.Channel;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.utils.TreeUtil;
import com.cj.sys.dao.security.UserDao;
import com.cj.sys.service.ServiceException;


@Service
@Transactional
public class ChannelManager extends EntityManager<Channel, Long> {
	@Autowired
	private ChannelDao channelDao;
	@Autowired
	private UserDao userDao;
	
	@Transactional(readOnly = true)
	public Channel getByCode(String code){
		try {
			return channelDao.findByUnique("code", code);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ServiceException("getByCode Error!" + e.getMessage(), e);
		}
	}
	@Transactional(readOnly = true)
	public List<Channel> getChildren(Long parentId){
		try {
			Criterion criterion = Restrictions.eq("parent.id", parentId);
			List<Order> orders = new LinkedList<Order>();
			orders.add(Order.asc("orderNum"));
			return channelDao.find(orders,criterion);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ServiceException("getByCode Error!" + e.getMessage(), e);
		}
	}
	@Override
	public void save(Channel entity) throws ServiceException {
		try {
			if(entity.getId() == null){
				entity.setCreateTime(new Date());
				entity.setCreateUser(userDao.getCurrentUser());
			}
			entity.setModifyTime(new Date());
			entity.setModifyUser(userDao.getCurrentUser());
			super.save(entity);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ServiceException("save Error!" + e.getMessage(), e);
		}
	}
	
	
	@Override
	protected ChannelDao getEntityDao() {
		return channelDao;
	}
	
	@Transactional(readOnly = true)
	public String getMenuXmlTree(Element root,Long parentId)throws Exception {
		try {
			boolean isRoot = false;
			Document doc = null;
			if(root == null){
				doc = DocumentHelper.createDocument();
				doc.setXMLEncoding("UTF-8");
				root = doc.addElement("TreeNode");
				isRoot = true;
			}
			if(parentId == null){
				parentId = 1L;
			}
			Channel channel = channelDao.get(parentId);
			Element element = TreeUtil.getElement(root, channel, "name", "id");
			if(channel.getSortChildren() != null){
				for(Channel chann : channel.getSortChildren()){
					getMenuXmlTree(element,chann.getId());
				}
			}
			if(isRoot)
				return doc.asXML();
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ServiceException(
					"getMenuXmlTree Error!" + e.getMessage(), e);
		}
	}
}
